package com.ymatou.shop.ui.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.ymatou.shop.Constants;
import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.Message;
import com.ymatou.shop.ui.adapter.BaseAdapter;
import com.ymatou.shop.ui.view.PullToRefreshView;
import com.ymatou.shop.ui.view.item.MessageItemView;
import com.ymatou.shop.util.DateUtil;

public abstract class BaseMessageActivity extends BaseHeaderListActivity {
	
	private static final int REQUEST_ID_LIST = 0x01;
	
	private BaseAdapter mAdapter;
	private List<Message> mList;
	
	private String mLastId;
	private volatile boolean mReachEnd;
	private volatile boolean mLoadingList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_message_list);
		
		mList = new ArrayList<Message>();
		mAdapter = new MessageAdapter(this, mList);
		
		setupViews();
		setupData(true);
	}
	
	@Override
	protected void setupViews() {
		super.setupViews();
		
		getTitleTextView().setText(R.string.title_message);
		
		if (getType() == Constants.TYPE_MESSAGE_BUYER_MESSAGE) {
			getTitleLeftImageButton().setImageResource(R.drawable.back_icon);
			getTitleLeftImageButton().setVisibility(View.VISIBLE);
			getTitleLeftImageButton().setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});
		}
		
		getListView().setAdapter(mAdapter);
		getPullToRefreshView().setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				mLastId = null;
				setupData(false);
			}
		});
		
		changeLoadingViewState(false);
	}
	
	@Override
	protected void setupData(boolean useCache) {
		if (!mReachEnd) {
			if (!mLoadingList) {
				RequestInfo requestInfo = ApiRequest.getMessageListRequest(getType(), Constants.PAGE_SIZE, mLastId);
				getCacheManager().register(REQUEST_ID_LIST, requestInfo, this);
			}
		}
	}
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo,
			Object response) {
		if (requestId == REQUEST_ID_LIST) {
			List<Message> list = ApiResponse.getMessageList((String) response);
			/////////////////test//////////
			Message message = new Message();
			message.mReplyInfo = new Message.ReplyInfo();
			message.mReplyInfo.mCommentId = "100";
			message.mReplyInfo.mUserName = "YYYYY";
			message.mReplyInfo.mContent = "评论开始，评论评论评论评论评论评论评论评论评论评论评论评论，评论结束";
			message.mReplyInfo.mTime = new Date(System.currentTimeMillis() - DateUtil.MILLIS_PER_HOUR - 10000);
			list.add(message);
			list.add(message);
			list.add(message);
			
			message = new Message();
			message.mReplyInfo = new Message.ReplyInfo();
			message.mReplyInfo.mCommentId = "1001";
			message.mReplyInfo.mUserName = "YYYYY";
			message.mReplyInfo.mContent = "评论开始，评论评论评论评论评论评论评论评论评论评论评论评论，评论结束";
			message.mReplyInfo.mTime = new Date(System.currentTimeMillis() - DateUtil.MILLIS_PER_DAY);
			message.mReplyToInfo = new Message.ReplyToInfo();
			message.mReplyToInfo.mReferId = "1001";
			message.mReplyToInfo.mContent = "回复开始，回复回复回复回复回复回复回复回复回复回复回复回复，回复结束";
			message.mReplyToInfo.mTime = new Date();
			message.mReplyToInfo.mUserName = "X名称";
			list.add(message);
			////////////////////////////////
			
			if (list == null) {
				if (mList.size() == 0) {
					changeErrorViewState(false);
				}
				return;
			}
			
			if (mList.size() == 0) {
				mList.addAll(list);
				getPullToRefreshView().setLastUpdated(new Date());
				
				changeLoadingViewState(true);
			} else {
				if (getPullToRefreshView().isRefresh()) {
					mList.clear();
				}
				mList.addAll(list);
				mAdapter.notifyDataSetChanged();
				getPullToRefreshView().onRefreshComplete(new Date());
			}
			
			if (list.isEmpty()) {
				mReachEnd = true;
			} else {
				mLastId = list.get(list.size() - 1).mReplyInfo.mCommentId;
			}
			mLoadingList = false;
		} else {
			super.onCacheSuccess(requestId, requestInfo, response);
		}
	}
	
	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo,
			CacheException error) {
		if (requestId == REQUEST_ID_LIST) {
			changeErrorViewState(false);
		} else {
			super.onCacheFailed(requestId, requestInfo, error);
		}
	}

	@Override
	public BaseAdapter getAdapter() {
		return mAdapter;
	}
	
	public abstract int getType();
	
	private class MessageAdapter extends BaseAdapter<Message> {

		public MessageAdapter(Context context, List<Message> items) {
			super(context, items);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MessageItemView itemView;
			if (convertView == null) {
				itemView = new MessageItemView(getContext());
			} else {
				itemView = (MessageItemView) convertView;
			}
			System.out.println("==============2");
			Message message = (Message) getItem(position);
			itemView.setData(message);
			
			return itemView;
		}
	}

}
